package com.shw.pclient.adapter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.shw.pclient.R;
import com.shw.pclient.vo.WorkingInfo;

/**
 * 
 * @author HAHA
 * 
 */
public class SHWWorkingAdapter extends BaseAdapter {

	private Context context;
	private List<WorkingInfo> workingInfos;
	private LayoutInflater layoutInflater;

	private class ListHolder {
		TextView workTextView;
		TextView workDateView;
	}

	public SHWWorkingAdapter(Context context) {
		super();
		this.context = context;
	}

	public void setList(List<WorkingInfo> workingInfos) {
		this.workingInfos = workingInfos;
		layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return workingInfos.size();
	}

	@Override
	public Object getItem(int arg0) {
		return workingInfos.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ListHolder listHolder;
		if (arg1 == null) {
			arg1 = layoutInflater.inflate(R.layout.work_item, null);
			listHolder = new ListHolder();
			listHolder.workTextView = (TextView) arg1
					.findViewById(R.id.shw_work);
			listHolder.workDateView = (TextView) arg1
					.findViewById(R.id.shw_work_date);
			arg1.setTag(listHolder);
		} else {
			listHolder = (ListHolder) arg1.getTag();
		}
		WorkingInfo workingInfo = workingInfos.get(arg0);
		if (workingInfo != null) {
			listHolder.workTextView.setText(workingInfo.getBody() + "...");
			listHolder.workDateView.setText(workingInfo.getDates());
		}
		return arg1;
	}

	public static List<WorkingInfo> getInfoFromFile() {
		File file = new File(Environment.getExternalStorageDirectory()
				.getPath() + "/SHW/Working/working.dat");
		File files = new File(Environment.getExternalStorageDirectory()
				.getPath() + "/SHW/Working");
		BufferedReader br = null;
		List<WorkingInfo> workingInfos = new ArrayList<WorkingInfo>();
		WorkingInfo wi = null;
		try {
			if (!files.exists()) {
				files.mkdirs();
			}
			br = new BufferedReader(new FileReader(file));
			String workBody = null;
			String workDate = null;
			if (br.ready()) {
				workBody = br.readLine();
				workDate = br.readLine();
				while (workBody != null) {
					wi = new WorkingInfo("", br.readLine(), "", br.readLine(),
							0);
					workingInfos.add(wi);
					workBody = br.readLine();
					workDate = br.readLine();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workingInfos;
	}
}
